interface DBProps {
    name: string;
}
class DB {
    constructor(props: DBProps) {
        this._name = props.name;
    }
    _name: string;
    openDB = (collectName: string) => {
        return new Promise((resolve, reject) => {
            const request = window.indexedDB.open(this._name, 1);
            request.onsuccess = function (e: any) {
                resolve(e.target.result);
            };
            request.onerror = function (e: any) {
                reject(e.target.result);
            };
            request.onupgradeneeded = function (e: any) {
                const db = e.target.result;
                if (!db.objectStoreNames.contains(collectName)) {
                    const objectStore = db.createObjectStore(collectName, {keyPath: 'id', autoIncrement: true});
                }
                resolve(e.target.result);
            };
        });
    };
    onPull = (collectName: string, index?: string) => {
        return new Promise((resolve, reject) => {
            this.openDB(collectName)
                .then((db: any) => {
                    if (index) {
                        //
                    } else {
                        const request = db.transaction(collectName).objectStore(collectName).openCursor();
                        const result: any = [];
                        request.onsuccess = function (res: any) {
                            if (res.target.result) {
                                const item = res.target.result;
                                result.push(item.value);
                                item.continue();
                            } else {
                                db.close();
                                resolve(result.reverse());
                            }
                        };
                    }
                })
                .catch((db) => {
                    throw db;
                });
        });
    };
    onPush = (collectName: string, data: any) => {
        return new Promise((resolve, reject) => {
            this.openDB(collectName)
                .then((db: any) => {
                    const request = db.transaction([collectName], 'readwrite').objectStore(collectName).add(data);
                    request.onsuccess = function () {
                        const ret = db.transaction(collectName).objectStore(collectName).openCursor();
                        const result: any = [];
                        ret.onsuccess = function (res: any) {
                            if (res.target.result) {
                                const item = res.target.result;
                                result.push(item.value);
                                item.continue();
                            } else {
                                db.close();
                                resolve(result.reverse());
                            }
                        };
                    };
                    request.onerror = function (err: any) {
                        reject(err);
                        db.close();
                        throw err;
                    };
                })
                .catch((err) => {
                    reject(err);
                    throw err;
                });
        });
    };
    onUpdate = (collectName: string, data: any) => {
        return new Promise((resolve, reject) => {
            this.openDB(collectName)
                .then((db: any) => {
                    const request = db.transaction([collectName], 'readwrite').objectStore(collectName).put(data);
                    request.onsuccess = function () {
                        const ret = db.transaction(collectName).objectStore(collectName).openCursor();
                        const result: any = [];
                        ret.onsuccess = function (res: any) {
                            if (res.target.result) {
                                const item = res.target.result;
                                result.push(item.value);
                                item.continue();
                            } else {
                                db.close();
                                resolve(result.reverse());
                            }
                        };
                    };
                    request.onerror = function (err: any) {
                        reject(err);
                        db.close();
                        throw err;
                    };
                })
                .catch((err) => {
                    reject(err);
                    throw err;
                });
        });
    };
    onDelete = (collectName: string, id: string, index?: string) => {
        return new Promise((resolve, reject) => {
            this.openDB(collectName)
                .then((db: any) => {
                    const request = db.transaction([collectName], 'readwrite').objectStore(collectName).delete(id);
                    request.onsuccess = function () {
                        const ret = db.transaction(collectName).objectStore(collectName).openCursor();
                        const result: any = [];
                        ret.onsuccess = function (res: any) {
                            if (res.target.result) {
                                const item = res.target.result;
                                result.push(item.value);
                                item.continue();
                            } else {
                                db.close();
                                resolve(result.reverse());
                            }
                        };
                    };
                    request.onerror = function (err: any) {
                        reject(err);
                        db.close();
                        throw err;
                    };
                })
                .catch((err) => {
                    reject(err);
                    throw err;
                });
        });
    };
}
export default DB;
